package com.wxd.demo.controller;

import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wxd.demo.entity.Test;
import com.wxd.demo.service.TestService;
import com.wxd.framework.common.FrameConstants;
import com.wxd.framework.common.ErrorConstants;
import com.wxd.framework.common.ServerInfo;
import com.wxd.framework.common.data.DateHelper;
import com.wxd.framework.controller.BaseController;

@Controller
@RequestMapping("/")
public class TestController extends BaseController  {
	
	@Autowired
	private TestService testService;
	
	@RequestMapping("/toTest.do")
	public String toTest(){
		return "test/test";
	}
	
	@RequestMapping("/getTestValue.do")
	public void getTestValue(HttpServletResponse response){
		String value = testService.getTestValue();
		super.renderText(response, value);
	}
	
	/**
	 * 解析 test excel文件，并展示到页面
	 * @param newFileName
	 * @param request
	 * @param response
	 * @throws IOException 
	 */
	@RequestMapping("/resolveDataFromServerFileForTest.do")
	public void resolveDataFromServerFileForTest(String newFileName, HttpServletRequest request, HttpServletResponse response) throws IOException{
		String serverPath = ServerInfo.getServerPath(request);
		boolean flag = false;
		List<Test> list = null;
		Map<Boolean, List<Test>> map = testService.resolveDataFromServerFileForTest(serverPath + "/" + newFileName);
		Iterator iterator = map.entrySet().iterator();
		if (iterator.hasNext()) {
			Map.Entry<Boolean, List<Test>> entry = (Map.Entry<Boolean, List<Test>>) iterator.next();
			flag = entry.getKey();
			list = entry.getValue();
		}else {
			flag = false;
			list = new ArrayList<>();
		}
		super.renderListWithBoolFlag(list, flag, response);			
	}
	
	/**
	 * 将解析数据保存到数据库
	 * @param jsonstr
	 * @param request
	 * @param response
	 */
	@RequestMapping("/saveTemplateDataForTest.do")
	public void saveTemplateDataForTest(String jsonstr, HttpServletRequest request, HttpServletResponse response){
		if (StringUtils.isNotBlank(jsonstr)) {
			String[] records = jsonstr.split(FrameConstants.RDRECORD_FLAG);
			if (records.length > 0) {
				try{
					testService.saveTemplateDataForTest(records);
					super.renderJsonExcuteSuccess(response);
				}catch(Exception ex){
					super.renderException(ex, response);
				}
			}else {
				super.renderJsonExcuteFail("无有效数据", response);
			}
			
		}else {
			super.renderJsonExcuteFail(ErrorConstants.ERROR_PARAM_MISS, response);
		}
	}
	
	/**
	 * 导出test信息
	 * @param request
	 * @param response
	 */
	@RequestMapping("/exportTestInfo.do")
	public void exportTestInfo(HttpServletRequest request, HttpServletResponse response){
		try {
			String fileName = "测试信息" + DateHelper.NowDateString();
			OutputStream outputStream = super.getExcelOutputStream(fileName, response);
			testService.exportTestInfo(outputStream);
			super.renderJsonExcuteSuccess(response);
		} catch (Exception ex) {
			super.renderException(ex, response);
		}
	}

}
